import * as osutil from 'node-os-utils';
import * as disk from 'node-disk-info';
import * as _ from 'lodash';

export class OsUtils {

  private cpu = osutil.cpu;
  private mem = osutil.mem;

  public async getCpuInfo() {
    const usage = await this.getCpuUsage();
    const count = this.cpu.count();
    return {
      usage,
      count
    };
  }

  public async getCpuUsage() {
    return await this.cpu.usage();
  }

  public getDriveInfo() {
    const disks = disk.getDiskInfoSync();
    let total = 0;
    let used = 0;
    let free = 0;
    let volumns = [];
    disks.forEach(disk => {
      const vTotalGb = _.toNumber((disk.blocks / 1024 / 1024 / 1024).toFixed(1));
      const vFreeGb = _.toNumber((disk.available / 1024 / 1024 / 1024).toFixed(1));
      const vUsedGb = _.toNumber((disk.used / 1024 / 1024 / 1024).toFixed(1));
      total += vTotalGb;
      used += vUsedGb;
      free += vFreeGb;
      volumns.push({
        'totalGb': vTotalGb,
        'usedGb': vUsedGb,
        'freeGb': vFreeGb,
        'name': disk.mounted,
        'usedPercentage': _.toNumber((vUsedGb / vTotalGb * 100).toFixed(2))
      });
    });
    return {
      'totalGb': _.toNumber(total.toFixed(1)),
      'usedGb': _.toNumber(used.toFixed(1)),
      'freeGb': _.toNumber(free.toFixed(1)),
      'usedPercentage': _.toNumber((used / total * 100).toFixed(2)),
      volumns
    };
  }

  public async getMemoryInfo() {
    return await this.mem.info();
  }
}